package com.fangtao.filter;

import com.fangtao.utils.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName PermissionsFilter
 * @Description TODO
 * @date 2022/9/22 21:08
 * @author: fangtao
 * @Version 1.0
 */
public class PermissionsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("登录状态过滤器开启");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest
//        HttpServletResponse
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
     if(req.getSession().getAttribute(Constants.USER_SESSION)==null){
         resp.sendRedirect("/smb/error.jsp");
     }
//     else {
//         System.out.println(req.getSession().getAttribute(Constants.USER_SESSION));
//     }
        System.out.println("登录状态过滤器执行");
        filterChain.doFilter(req,resp);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
